import java.io.*;
import java.util.*;
/**
 * @author Patrick Bartylla
 * @version 1
 * @Date 21.05.2013
 */
public class TextFileWriter {

	private static DataManager dataManager = DataManager.getDataManagerInstance();

	public TextFileWriter()
	{
		
	}
	
	/**
	 * Feature Vector Writer
	 * @throws Exception
	 */
	public void WriteFeatureVector() throws Exception
	{
		//Initialization
		File myFile = null;
		BufferedWriter out = null;
		
		
		try {		
			//Creating a new file
			String s = dataManager.getOutputDataDirectory().toString();
			s = s.replaceAll("%20", " ");
			
			myFile = new File(s, "FV.txt" );
		    
			if (myFile.exists())  
		    {
		    	myFile.delete();
		    }
			
		    myFile.createNewFile();
		    
		    //Writing First Line
		    out = new BufferedWriter (new FileWriter(myFile));
			out.write(dataManager.getFeatureVectorAcronym());
			out.newLine();
			
			 //Writing Feature Vector
			for(int i = 0; i < dataManager.getFeatureWords().size(); i++)
			{
				out.write(dataManager.getFeatureWords().get(i));
				out.newLine();
			}		    
		}
		catch (Exception e)
		{
			throw e;
		}
		finally
		{
			out.close();
		}
	}
	
	/**
	 * ARFF-TXT File Writer
	 * @throws Exception
	 */
	public void WriteARFF() throws Exception
	{
		//Initialization
		File myFile = null;
		BufferedWriter out = null;
		
		
		try {		
			//Creating a new file
			String s = dataManager.getOutputDataDirectory().toString();
			s = s.replaceAll("%20", " ");
			
			myFile = new File(s, "ARFF.txt" );
			
		    if (myFile.exists())  
		    {
		    	myFile.delete();
		    }
		    
		    myFile.createNewFile();
		    
		    out = new BufferedWriter (new FileWriter(myFile));
			//out.write(dataManager.getARFFAcronym());
			//out.newLine();
				
		    
		    //Writing Arff Header	
		    out.write("@relation classifier");

			out.newLine();
			out.newLine();
					
			for(int i = 0; i < dataManager.getFeatureWords().size(); i++)
			{
				out.write("@attribute ");
				out.write(dataManager.getFeatureWords().get(i));
				out.write(" integer");
			
				out.newLine();
			}
			
			out.write("@attribute \'FVclass\' {");
			
			for(int i = 0; i < dataManager.getClassesCount(); i++)
			{
				if(i != dataManager.getClassesCount()-1)
					out.write(i + ", ");
				else
					out.write(Integer.toString(i));
			}
			
			out.write("}");
			out.newLine();
			out.newLine();
			out.write("@data");
			out.newLine();	
		    
			//Writing ARFF Data
			for(int i = 0; i < dataManager.getARFFdata().size(); i++)
			{
				ArrayList<Integer> row = dataManager.getARFFdata().get(i);
				for(int j = 0; j<row.size(); j++)
				{
					out.write(row.get(j).toString());
					out.write(", ");
				}
				
				//Write new " "
				//out.write(" ");
				//Writing Class 

				int j = -1;
				boolean found = false;
				
				do{
					j++;
					if(dataManager.getARFFRowClass().get(i).equals(dataManager.getAllClasses().get(j)))
					{
						found = true;
					}
					
				}while(!found);
				
				
				out.write(Integer.toString(j)); 

				out.newLine();
			}
		}
		catch (Exception e)
		{
			throw e;
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}

	
	/**
	 * ARFF-Question-TXT File Writer
	 * @throws Exception
	 */
	public void WriteQuestionARFF() throws Exception
	{
		//Initialization
		File myFile = null;
		BufferedWriter out = null;
		
		
		try {		
			//Creating a new file
			String s = dataManager.getOutputDataDirectory().toString();
			s = s.replaceAll("%20", " ");
			
			myFile = new File(s, "ARFF_Question.txt" );
			
		    if (myFile.exists())  
		    {
		    	myFile.delete();
		    }
		    
		    myFile.createNewFile();
		    
		    out = new BufferedWriter (new FileWriter(myFile));
			//out.write(dataManager.getARFFAcronym());
			//out.newLine();
				
		    
		    //Writing Arff Header	
		    out.write("@relation classifier");

			out.newLine();
			out.newLine();
					
			for(int i = 0; i < dataManager.getFeatureWords().size(); i++)
			{
				out.write("@attribute ");
				out.write(dataManager.getFeatureWords().get(i));
				out.write(" integer");
			
				out.newLine();
			}
			
			out.write("@attribute \'FVclass\' {");
			
			for(int i = 0; i < dataManager.getClassesCount(); i++)
			{
				if(i != dataManager.getClassesCount()-1)
					out.write(i + ", ");
				else
					out.write(Integer.toString(i));
			}
			
			out.write("}");
			out.newLine();
			out.newLine();
			out.write("@data");
			out.newLine();	
		    
			//Writing ARFF Data
			for(int i = 0; i < dataManager.getQuestionARFFdata().size(); i++)
			{
				ArrayList<Integer> row = dataManager.getQuestionARFFdata().get(i);
				for(int j = 0; j<row.size(); j++)
				{
					out.write(row.get(j).toString());
					out.write(",");
				}
				
				//Write new " "
				//out.write(" ");
				//Writing Class 

				int j = -1;
				boolean found = false;
				
				do{
					j++;
					if(dataManager.getQuestionARFFRowClass().get(i).equals(dataManager.getAllClasses().get(j)))
					{
						found = true;
					}
					
				}while(!found);
				
				
				out.write(Integer.toString(j)); 

				out.newLine();
			}
		}
		catch (Exception e)
		{
			throw e;
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}
	
}